package com.ruoyi.mp.listener;


import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.db.domain.system.MpClientOrder;
import com.ruoyi.mp.service.IMpClientOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * redis失效数据监听，监听未支付订单
 * @Author lililiang
 * @Date 2022/6/27 9:37
 */

@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Autowired
    IMpClientOrderService mpClientOrderService;
    @Autowired
    RedisService redisService;
    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    /**
     * 针对redis数据失效事件，进行数据处理
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 1 获取失效key
        String expiredKey = message.toString();
        String value = new String(message.getBody());
        System.out.println(value);
        System.out.println(expiredKey);
        System.out.println(message.getChannel());
        if(value!=null){
            //订单超时,删除待支付订单
            MpClientOrder order = mpClientOrderService.selectMpClientOrderByOrderId(expiredKey);
            if(order!=null&&order.getOrderStatus().equals(101l)){
                order.setOrderStatus(501l);
                mpClientOrderService.updateMpClientOrder(order);
            }
        }

    }
}
